/*
 * @Date: 2025-02-27 10:28:34
 * @Auth: 873768511@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-09-28 14:17:14
 * @FilePath: \corpus\src\pages\resourceManagement\fileList\hooks\index.ts
 */
import type { ActionType, FormInstance } from '@ant-design/pro-components';
import { useRequest } from 'ahooks';
import { createContainer } from 'unstated-next';
// import {

// } from '@/services/rulesCenter/modelManagement/list';
import {
  addCorpusFile,
  addNewKnowledge,
  addResourceFile,
  addResourceFileKnowledge,
  deleteResourceFile,
  getCorpusBaseList,
  getNewList,
  getPrompt,
  getResourceFile,
  parseBatch,
} from '@/services/ant-design-pro/resource';
import { Form, message } from 'antd';

import { useLocation, useModel } from '@umijs/max';
import { useRef, useState } from 'react';
export default createContainer(() => {
  const [url, setUrl] = useState<string>('');
  const [type, setType] = useState<string>('');
  const [jsonViewVisible, setJsonViewVisible] = useState<boolean>(false);
  const [viewVisible, setViewVisible] = useState<boolean>(false);
  const [createCorpusVisible, setCreateCorpusVisible] = useState<boolean>(false);
  const [analysisVisible, setAnalysisVisible] = useState<boolean>(false);
  const [createKnowledgeVisible, setCreateKnowledgeVisible] = useState<boolean>(false);
  const [exportFileVisible, setExportFileVisible] = useState<boolean>(false);
  const [modalKnowledgeVisible, setModalKnowledgeVisible] = useState(false);

  const location: Record<string, any> = useLocation();

  const id = location?.state?.id ? location?.state?.id : '';
  const tableRef = useRef<ActionType>();
  const formRef = useRef<FormInstance>();

  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState || {};

  const [formAnalysis] = Form.useForm();
  const [exportFile] = Form.useForm();
  const [forKnowledgemRef] = Form.useForm();
  const [corpusForm] = Form.useForm();

  const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  const [selectedRows, setSelectedRows] = useState<React.Key[]>([]);
  const [newList, setNewList] = useState<any[]>([]);
  const [corpusList, setCorpusList] = useState<any[]>([]);

  // 删除
  const deleteRule = useRequest(
    async (id) => {
      const res = await deleteResourceFile({ ids: id });
      if (res?.success) {
        message.success('删除成功');
        tableRef?.current?.reload();
      }
    },
    { manual: true },
  );

  const addNewKnowledgeContent = useRequest(
    async (val) => {
      const res = await addNewKnowledge({
        ...val,
        deptCode: currentUser?.deptCode,
      });
      if (res?.success) {
        message.success('添加成功');
        setModalKnowledgeVisible(false);
      }
    },
    { manual: true },
  );
  const addCorpusFileContent = useRequest(
    async (val) => {
      const res = await addCorpusFile({
        ...val,
        resourceId: id,
      });
      if (res?.success) {
        message.success('添加成功');
        setCreateCorpusVisible(false);
      }
    },
    { manual: true },
  );

  const addResourceFileContent = useRequest(
    async (val) => {
      const res = await addResourceFile({ ...val, resourceId: id });
      if (res?.success) {
        message.success('导入成功');
        tableRef?.current?.reload();
        setExportFileVisible(false);
      }
    },
    { manual: true },
  );

  const getNewListContent = useRequest(
    async (val) => {
      const res = await getNewList(val);
      if (res?.data) {
        setNewList(res.data);
      }
    },
    { manual: true },
  );

  const getCorpusBase = useRequest(
    async ({ ...query }) => {
      const res = await getCorpusBaseList({
        ...query,
      });
      if (res?.data) {
        setCorpusList(res?.data);
      }
    },
    {
      manual: true,
    },
  );
  const addResourceFileKnowledgeContent = useRequest(
    async (id) => {
      const res = await addResourceFileKnowledge(id);
      if (res?.success) {
        message.success('新增成功');
        setCreateKnowledgeVisible(false);
        tableRef?.current?.reload();
      }
    },
    { manual: true },
  );

  const parseBatchContent = useRequest(
    async (id) => {
      const res = await parseBatch(id);
      if (res?.success) {
        message.success('解析成功');
        setAnalysisVisible(false);
        tableRef?.current?.reload();
      }
    },
    { manual: true },
  );

  const getPromptContent = useRequest(
    async (type) => {
      const res = await getPrompt(type);
      return res.data;
    },
    { manual: true },
  );

  const getTableList = useRequest(
    async ({ ...query }) => {
      const res = await getResourceFile({
        ...query,
        resourceId: id,
      });
      return {
        data: res?.data || [],
        total: res?.total || 0,
        success: res?.success,
      };
    },
    {
      manual: true,
    },
  );

  const resourceDetail = useRequest(async () => {
    // const res = await modelDetail({ irsCode: query.irsCode });
    // return res.data
  });
  return {
    tableRef,
    formRef,
    deleteRule,
    getTableList,
    resourceDetail,
    selectedRowKeys,
    setSelectedRowKeys,
    selectedRows,
    setSelectedRows,

    viewVisible,
    setViewVisible,
    url,
    setUrl,
    setType,
    type,
    jsonViewVisible,
    setJsonViewVisible,
    analysisVisible,
    setAnalysisVisible,

    formAnalysis,
    addResourceFileContent,
    exportFile,
    exportFileVisible,
    setExportFileVisible,

    addResourceFileKnowledgeContent,
    newList,
    getNewListContent,
    setNewList,
    addNewKnowledgeContent,
    modalKnowledgeVisible,
    setModalKnowledgeVisible,
    addNewKnowledge,
    forKnowledgemRef,
    createCorpusVisible,

    addCorpusFileContent,
    corpusForm,
    setCreateCorpusVisible,
    getCorpusBase,
    corpusList,
    getPromptContent,
    createKnowledgeVisible,
    setCreateKnowledgeVisible,
    parseBatchContent,
  };
});
